package cn.iocoder.yudao.module.pay.wallet.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.module.api.brokerage.record.message.CommissionCancelMessage;
import cn.iocoder.yudao.module.api.brokerage.record.message.CommissionUnfreezeMessage;
import cn.iocoder.yudao.module.api.pay.wallet.PayWalletApi;
import cn.iocoder.yudao.module.api.pay.wallet.dto.PayWalletAddBalanceReqDTO;
import cn.iocoder.yudao.module.api.pay.wallet.dto.PayWalletReduceBalanceReqDTO;
import cn.iocoder.yudao.module.api.pay.wallet.enums.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.api.promotion.gift.message.OnlineProductGiftTakeMessage;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueCancelMessage;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueUnfreezeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/05/23
 */
@Component
@Slf4j
public class WalletConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(CommissionUnfreezeMessage message) {
        log.info("[pay/wallet][onMessage][推广佣金结算事件({})]", message);
        PayWalletAddBalanceReqDTO dto = new PayWalletAddBalanceReqDTO();
        dto.setUserId(message.getUserId());
        dto.setUserType(UserTypeEnum.MEMBER.getValue());
        dto.setBizType(PayWalletBizTypeEnum.COMMISSION_UNFREEZE.getValue());
        dto.setBizId(message.getTradeNo());
        dto.setPrice(message.getSettlementMoney());
        SpringUtil.getBean(PayWalletApi.class).addWalletBalance(dto);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(CommissionCancelMessage message) {
        log.info("[pay/wallet][onMessage][推广佣金取消事件({})]", message);
        PayWalletReduceBalanceReqDTO dto = new PayWalletReduceBalanceReqDTO();
        dto.setUserId(message.getUserId());
        dto.setUserType(UserTypeEnum.MEMBER.getValue());
        dto.setBizType(PayWalletBizTypeEnum.COMMISSION_CANCEL.getValue());
        dto.setBizId(message.getTradeNo());
        dto.setPrice(message.getSettlementMoney());
        SpringUtil.getBean(PayWalletApi.class).reduceWalletBalance(dto);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueUnfreezeMessage message) {
        log.info("[pay/wallet][onMessage][商家营收结算事件({})]", message);
        Long userId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(message.getShopId());
        PayWalletAddBalanceReqDTO balanceReqDTO = new PayWalletAddBalanceReqDTO();
        balanceReqDTO.setUserId(userId);
        balanceReqDTO.setUserType(UserTypeEnum.MEMBER.getValue());
        balanceReqDTO.setBizType(PayWalletBizTypeEnum.REVENUE_UNFREEZE.getValue());
        balanceReqDTO.setBizId(message.getTradeNo());
        balanceReqDTO.setPrice(message.getSettlementMoney());
        SpringUtil.getBean(PayWalletApi.class).addWalletBalance(balanceReqDTO);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueCancelMessage message) {
        log.info("[pay/wallet][onMessage][商家营收取消事件({})]", message);
        Long userId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(message.getShopId());
        PayWalletReduceBalanceReqDTO balanceReqDTO = new PayWalletReduceBalanceReqDTO();
        balanceReqDTO.setUserId(userId);
        balanceReqDTO.setUserType(UserTypeEnum.MEMBER.getValue());
        balanceReqDTO.setBizType(PayWalletBizTypeEnum.REVENUE_CANCEL.getValue());
        balanceReqDTO.setBizId(message.getTradeNo());
        balanceReqDTO.setPrice(message.getSettlementMoney());
        SpringUtil.getBean(PayWalletApi.class).reduceWalletBalance(balanceReqDTO);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(OnlineProductGiftTakeMessage message) {
        log.info("[pay/wallet][onMessage][线上产品类型的赠品领取事件({})]", message);
        Long userId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(message.getShopId());
        PayWalletAddBalanceReqDTO balanceReqDTO = new PayWalletAddBalanceReqDTO();
        balanceReqDTO.setUserId(userId);
        balanceReqDTO.setUserType(UserTypeEnum.MEMBER.getValue());
        balanceReqDTO.setBizType(PayWalletBizTypeEnum.REVENUE_GIFT.getValue());
        balanceReqDTO.setBizId(message.getTradeNo());
        balanceReqDTO.setPrice(message.getSettlementMoney());
        SpringUtil.getBean(PayWalletApi.class).addWalletBalance(balanceReqDTO);
    }

}
